import time

import requests
import pickle
import json
from bs4 import BeautifulSoup
from concurrent.futures import ThreadPoolExecutor, wait

page_url = 'https://ssr1.scrape.center/page/{}'
index_url = 'https://ssr1.scrape.center'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36'
}


def get_html(url):
    response = requests.get(url, headers=headers)
    return BeautifulSoup(response.text, 'lxml')


def parse_html(index):
    print(f'开始爬取第{index}页数据...................')
    soup = get_html(page_url.format(index))
    divs = soup.find('div', class_='el-col el-col-18 el-col-offset-3').find_all('div', class_='el-card item m-t is-hover-shadow')
    with ThreadPoolExecutor() as pool:
        pool.map(parse_detail, divs)


def parse_detail(div):
    print('开始解析div............')
    # info
    infos = list(div.stripped_strings)
    # 标题
    # title = div.find('a', class_='name').find('h2').string
    title = infos[0]
    # 详情url
    detail_url = index_url + div.a['href']
    # 剧情简介
    detail_soup = get_html(detail_url)
    profile = list(detail_soup.find('div', class_='drama').p.stripped_strings)[0]
    # 封面
    cover_img = div.a.find('img')['src']
    # categories 分类
    # categories = [item.string for item in div.find('div', class_='categories').find_all('span')]
    category = infos[1: -5]
    # 上映信息
    release_info = ' '.join(infos[-5: -1])
    # 评分
    score = infos[-1]
    result.append({
        'title': title,
        'detail_url': detail_url,
        'profile': profile,
        'cover_img': cover_img,
        'category': category,
        'release_info': release_info,
        'score': score
    })


if __name__ == '__main__':
    result = []
    time1 = time.time()
    with ThreadPoolExecutor() as executor:
        tasks = executor.map(parse_html, range(1, 11))
        wait(tasks)
    with open('movies.json', 'w', encoding='utf-8') as f:
        json.dump(result, f, ensure_ascii=False, indent=4)
    time2 = time.time()
    print(f'总用时{time2 - time1}s')
